package demo4;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2023-12-11
 * Time: 17:05
 */
public class MyStack {
    private  int[] elem;
    private int usedSize;

    private MyStack(){
        this.elem = new int[5];

    }

    public void push(int val){
        if (isFull()) {
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }
    public boolean isFull(){
        return usedSize == elem.length;
    }

    public void pop(){
        if (empty()){
            throw new StackEmptyException("栈为空！");
        }
        return elem[--usedSize];
    }

    public int peak(){
        if (empty()){
            throw new StackEmptyException("栈为空！")
        }
        return elem[usedSize-1];
    }

    public boolean empty(){
        return usedSize == 0;
    }

}
